﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace LabMS.Web.Parameters
{
    public partial class ParameterSetting : Base.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            strPagePrivilege.Add("CSSZ");
            strPageUserType.Add("mana");
            if (!IsPostBack)
            {
                bool a = false;
                string str = "select * from SysSetting";
                a = Maticsoft.DBUtility.DbHelperSQL.Exists(str);
                if (a == true)//有记录
                {
                    InitBind();
                    hd.Value = "update";
                }
                else
                {
                    hd.Value = "insert";
                    BindDDL();
                    ddlYear.SelectedValue= DateTime.Now.Year.ToString();
                }
                
            }
        }

        /// <summary>
        /// 页面初始化
        /// </summary>
        protected void InitBind()
        {
            BindDDL();
            LabMS.BLL.SysSetting BSY = new LabMS.BLL.SysSetting();
            DataSet ds = new DataSet();
            ds=BSY.GetAllList();
            ddlYear.SelectedValue=ds.Tables[0].Rows[0]["CurrentYear"].ToString();
            ddlSem.SelectedValue = ds.Tables[0].Rows[0]["CurrentSemester"].ToString();
            if (ds.Tables[0].Rows[0]["ExperimentApproval"].ToString() == "1")
            {
                rb1.Checked = true;
            }
            else
            {
                rb11.Checked = true;
            }
            if (ds.Tables[0].Rows[0]["ExperimentAutoGenerateID"].ToString() == "1")
            {
                rb2.Checked = true;
            }
            else
            {
                rb22.Checked = true;
            }
            tb1.Text = ds.Tables[0].Rows[0]["ExperimentBookForward"].ToString();
            tb2.Text = ds.Tables[0].Rows[0]["ExperimentCancleForward"].ToString();
            if (ds.Tables[0].Rows[0]["PreparationReportRequired"].ToString() == "1")
            {
                rb3.Checked = true;
            }
            else
            {
                rb33.Checked = true;
            }
            tb3.Text = ds.Tables[0].Rows[0]["PreparationReportSubmitForward"].ToString();
            tb4.Text = ds.Tables[0].Rows[0]["PassedLimit"].ToString();
            if (ds.Tables[0].Rows[0]["ScoreType"].ToString() == "1")
            {
                rb4.Checked = true;//1代表百分制，2代表等级制
            }
            else
            {
                rb44.Checked = true;
            }
        }

        /// <summary>
        /// 下拉框绑定
        /// </summary>
        protected void BindDDL()
        {
            LabMS.BLL.Semester BSemester = new LabMS.BLL.Semester();
            DataSet ds = new DataSet();
            string str = "select distinct Year from Semester";
            ds = Maticsoft.DBUtility.DbHelperSQL.Query(str);
            ddlYear.DataSource = ds.Tables[0].DefaultView;
            ddlYear.DataTextField = "Year";
            ddlYear.DataValueField = "Year";
            ddlYear.DataBind();

            string str2 = "select distinct MSemester from Semester";
            DataSet ds2 = new DataSet();
            ds2 = Maticsoft.DBUtility.DbHelperSQL.Query(str2);
            for (int i = ds2.Tables[0].Rows.Count - 1; i > -1; i--)
            {
                if (ds2.Tables[0].Rows[i]["MSemester"].ToString() == "1")
                {
                    ddlSem.Items.Add(new ListItem("春季", "1"));
                }
                else if(ds2.Tables[0].Rows[i]["MSemester"].ToString()=="2")
                {
                    ddlSem.Items.Add(new ListItem("秋季", "2"));
                }
            }
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            #region 验证
            if (string.IsNullOrEmpty(tb1.Text.Trim()))
            {
                Common.JShelper.JSAlert(this.Page,"","实验预约应提前几天不能为空！");
                tb1.Focus();
            }
            else if (string.IsNullOrEmpty(tb2.Text.Trim()))
            {
                Common.JShelper.JSAlert(this.Page, "", "取消预约应提前几天不能为空！");
                tb2.Focus();
            }
            else if (string.IsNullOrEmpty(tb3.Text.Trim()))
            {
                Common.JShelper.JSAlert(this.Page, "", "学生提交预习报告应提前几天不能为空！");
                tb3.Focus();
            }
            else if(string.IsNullOrEmpty(tb4.Text.Trim()))
            {
                Common.JShelper.JSAlert(this.Page, "", "成绩的及格分数线不能为空！");
                tb4.Focus();
            }
            #endregion
            else
            {
                LabMS.Model.SysSetting MSysSetting = new LabMS.Model.SysSetting();
                ExtendBLL.SysSetting eSys = new ExtendBLL.SysSetting();
                MSysSetting.ID = eSys.GetIDByDDLYear(int.Parse(ddlYear.SelectedValue.ToString()));//根据学年取ID
                MSysSetting.CurrentYear = int.Parse(ddlYear.SelectedValue.ToString());
                MSysSetting.CurrentSemester = int.Parse(ddlSem.SelectedValue.ToString());
                MSysSetting.ExperimentApproval = rb1.Checked ? true : false;   //1为是，0为否
                MSysSetting.ExperimentAutoGenerateID = rb2.Checked ? true:false;
                MSysSetting.ExperimentBookForward =int.Parse(tb1.Text.Trim().ToString());
                MSysSetting.ExperimentCancleForward =int.Parse(tb2.Text.Trim().ToString());
                MSysSetting.PreparationReportRequired = rb3.Checked ? true : false;
                MSysSetting.PreparationReportSubmitForward =int.Parse(tb3.Text.Trim().ToString());
                MSysSetting.PassedLimit =decimal.Parse(tb4.Text.Trim());
                MSysSetting.ScoreType = rb4.Checked ? 1 : 2;//1代表百分制，2代表等级制

                LabMS.BLL.SysSetting BS = new LabMS.BLL.SysSetting();

                if (hd.Value == "update")
                {
                    LabMS.BLL.SysSetting bS = new LabMS.BLL.SysSetting();
                    bool success = false;
                    try
                    {
                        bS.Update(MSysSetting);
                        success = true;
                    }
                    catch(Exception ex)
                    {
                        throw ex;
                    }
                    if (success==true)
                    {
                        Common.JShelper.JSAlert(this.Page, "", "保存成功！");
                        InitBind();
                    }
                    else
                    {
                        Common.JShelper.JSAlert(this.Page, "", "保存失败！");
                    }
                }
                else if(hd.Value=="insert")
                {
                    int count=BS.Add(MSysSetting);
                    if (count > 0)
                    {
                        Common.JShelper.JSAlert(this.Page, "", "保存成功！");
                    }
                    else
                    {
                        Common.JShelper.JSAlert(this.Page, "", "保存失败！");
                    }
                }
                
            }
        }
    }
}
